﻿using System.Web;

namespace YawetagLib.Web.Utilities
{
    public static class ResourceProvider
    {
        private const string TEMP_RELATIVE_PATH = "../../";

        public static string GetResourcePath(string file, bool tempRelative)
        {
            return string.Format("{0}resources/{1}", tempRelative ? TEMP_RELATIVE_PATH : GetApplicationPath(), file);
        }

        public static string GetResourcePath(string file)
        {
            return GetResourcePath(file, false);
        }

        public static string GetLocalResourcePath(string path, string file, bool tempRelative)
        {
            path = path.Replace("~/", string.Empty);
            string localPath = path.Substring(0, path.LastIndexOf("/", System.StringComparison.Ordinal) + 1);

            return string.Format("{0}{1}resources/{2}", tempRelative ? TEMP_RELATIVE_PATH : GetApplicationPath(), localPath, file);
        }

        public static string GetLocalResourcePath(string path, string file)
        {
            return GetLocalResourcePath(path, file, false);
        }

        public static string GetActualPath(string path)
        {
            return HttpContext.Current.Request.MapPath("~" + path);
        }

        public static string GetActualResourcePath(string file)
        {
            return GetActualPath("/resources/" + file);
        }

        public static string GetApplicationPath()
        {
            return HttpContext.Current.Request.ApplicationPath.EndsWith("/")
                ? HttpContext.Current.Request.ApplicationPath
                : HttpContext.Current.Request.ApplicationPath + "/";
        }
    }
}
